package com.zhx.demo.service.impl;

import com.ygsoft.activiti.service.impl.RuntimeServiceImpl;
import com.zhx.demo.constants.DemoConstans;
import com.zhx.demo.entity.Leave;
import com.zhx.demo.reposirory.LeaveRepository;
import com.zhx.demo.service.LeaveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhx
 * @create 2017-11-15 10:43
 **/
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveRepository leaveRepository;

    @Autowired
    private RuntimeServiceImpl runtimeService;

    @Override
    public boolean startLeave(Leave leave,String userId) {
        //开始保存业务优先
        Leave save = leaveRepository.save(leave);
        //开始发起流程
        String s = runtimeService.startProcessByKey(userId, DemoConstans.PROCESS_LEAVE, Long.toString(leave.getId()), null);

        leave.setProcess_instance_id(s);
        leaveRepository.save(leave);

        return true;
    }

    @Override
    public List<Leave> getLeaveList(String userId) {
        if(StringUtils.isNotBlank(userId)){
            List<Leave> leaveByUser_id = leaveRepository.getLeaveByUserId(userId);
            return leaveByUser_id;
        }else{
            return leaveRepository.findAll();
        }
    }

    @Override
    public Leave getLeaveDetail(String leaveId) {
        return null;
    }
}
